#!/bin/bash
MACHINES="NovaNanoSEM Quanta200";
ALLMACHINES="NovaNanoSEM Quanta200";
HOME="/usr/local/microtardis/harvest/new";
DIR_COLLATION="${HOME}/mnt/rmmf_collation";           # where raw data is rsynced to
DIR_STAGING="${HOME}/mnt/rmmf_staging";               # where symlinks in "flipped structure" are created
LOGAREA="${HOME}/logs";
OUTPUTTEMP="${LOGAREA}/current.log";
OUTPUTDAILY="${LOGAREA}/rsync`date +\%Y_\%m_\%d`.log";
OUTPUTSYMLK="${LOGAREA}/rsync_current.log";

RM="/bin/rm"

# -v, --verbose                           increase verbosity
# -c, --checksum                          skip based on checksum, not mod-time & size
# -r, --recursive                         recurse into directories
# -l, --links                             copy symlinks as symlinks
# -t, --times                             preserve times
# -S, --sparse                            handle sparse files efficiently
# -h, --human-readable                    output numbers in a human-readable format
# --delete                                delete files that don't exist on sender
# --progress                              show progress during transfer
# --exclude                               don't copy junk files (like Windows thumbnails)
# --modify-window                         compares times with less accuracy (supposed to be good for Windows systems)
RSYNCOPTIONS="-rltSh --modify-window=2 --progress --exclude 'Thumbs.db'";

RSYNC="/usr/bin/rsync";

function summarylog {
echo "`date` $1" >> ${LOGAREA}/summary.log
}

function rsyncPC2HERE {
echo "RsyncPC2HERE"

MACHINE=$1;
unset MACHINEFOLDER
unset MACHINEIP
if [ $MACHINE == "NovaNanoSEM" ]; then
  MACHINEFOLDER="SharedData"
  MACHINEIP="192.168.10.23"
elif [ $MACHINE == "Quanta200" ]; then
  MACHINEFOLDER="UsersImages"
  MACHINEIP="192.168.10.24"
else
  echo "Unknown machine: ${MACHINE}" 2>&1
  return 
fi


${RM} ${OUTPUTTEMP};

echo -e "\/n" > ${OUTPUTTEMP};
echo "Rsync starting at `/bin/date`" >> ${OUTPUTTEMP};

#checkmount ${DIR_INSTR};


# only copy directories with names like e1234, s0189 etc.
 
TIMERSTART=`date +%s`
FILESIZEBEFORE=`du -ch ${DIR_COLLATION}/${MACHINE} | tail -n 1 | awk '{print $1}'`

# perform rsync
${RSYNC} ${RSYNCOPTIONS} --include '/[eEzZxXsS][0-9][0-9]*' --exclude '/*' "rsync://${MACHINEIP}/${MACHINEFOLDER}/*" ${DIR_COLLATION}/${MACHINE} >> ${OUTPUTTEMP} 2>&1;

#${RSYNC} ${RSYNCOPTIONS} $DIRS2SYNC ${DIR_COLLATION}/${MACHINE} >> ${OUTPUTTEMP} 2>&1;


STATUS=$?;
ELAPSEDTIME=`expr \`date +%s\` - ${TIMERSTART}`
FILESIZEAFTER=`du -ch ${DIR_COLLATION}/${MACHINE} | tail -n 1 | awk '{print $1}'`

if [ ${STATUS} -eq 0 ]; then
    echo "Rsync stopped (successfully) at `/bin/date`" >> ${OUTPUTTEMP};
    ./set_status.sh ${MACHINE}_rsync ok "Rsync completed successfully."
    summarylog "(${ELAPSEDTIME}s) ${MACHINE} rsync finished. (Before: ${FILESIZEBEFORE}, after: ${FILESIZEAFTER})."
else
    echo "Rsync stopped (with error ${STATUS}) at `/bin/date`" >> ${OUTPUTTEMP};
    ./set_status.sh ${MACHINE}_rsync down "Rsync stopped (with error ${STATUS}) at `/bin/date`"
    summarylog "(${ELAPSEDTIME}s) Rsync for ${MACHINE} failed with error ${STATUS}. (Before: ${FILESIZEBEFORE}, after: ${FILESIZEAFTER}."
fi

echo -e \
"\n--------------------------------------------------------------\n" \
                   >> ${OUTPUTTEMP};

/bin/cat ${OUTPUTTEMP} >> ${OUTPUTDAILY};
${RM} ${OUTPUTSYMLK};
/bin/ln -s ${OUTPUTDAILY} ${OUTPUTSYMLK};
${RM} ${OUTPUTTEMP};

}

function flip {

MACHINE=$1;
TIMERSTARTED=`date +%s`
echo "Creating flipped structure for ${MACHINE}." >> ${OUTPUTTEMP};

# Set the field separator to a newline
IFS="
"

if [ -d /tmp/dir.out ];then
     $RM /tmp/dir.out;
fi

pushd ${DIR_COLLATION}/${MACHINE}

find . -maxdepth 1 -type d | sed -e "s/^\.\///" | grep -v "^\.$"| sort \
    > /tmp/dir.out;

popd

for USERDIR in `cat /tmp/dir.out`;do
     mkdir -p ${DIR_STAGING}/${USERDIR}/${MACHINE};
    # Temporarily replacing symlink with copy operation. One downside of symlinking is that it forces the directory on ka
    # (/mnt/rmmf_collation/subset...) to match the directory name on rmmf, or the symlink breaks.

    #ln -s ${DIR_COLLATION}/${MACHINE}/${USERDIR}/* ${DIR_STAGING}/${USERDIR}/${MACHINE};
    # cp -R ${DIR_COLLATION}/${MACHINE}/${USERDIR}/* ${DIR_STAGING}/${USERDIR}/${MACHINE};
    rsync -vcrtSh ${DIR_COLLATION}/${MACHINE}/${USERDIR}/* ${DIR_STAGING}/${USERDIR}/${MACHINE}
    # We now make the file group readwriteable because on rmmf, the apache user is in the rmmf group, which has the same id as the rmmf group on this machine.

    chmod -R g+rw ${DIR_STAGING}/${USERDIR}/${MACHINE}/*;
    # Make directories group executable
    find ${DIR_STAGING}/${USERDIR}/${MACHINE} -type d -exec chmod 770 {} \;
done

ELAPSEDTIME=`expr \`date +%s\` - ${TIMERSTARTED}`

echo "Done." >> ${OUTPUTTEMP};
summarylog "(${ELAPSEDTIME}s) Created flipped structure for ${MACHINE} under ${DIR_STAGING}."
}

if [ -e flip.running ]; then
	echo "Flip script already running - abort.";
	exit 1;
fi
cd ${HOME}
echo "Started running flip script at `date`" > flip.running
echo ${OUTPUTTEMP}
echo "------------- `date` -----------" >> ${OUTPUTTEMP}
echo "Flip script with arguments: $@" >> ${OUTPUTTEMP}

if [ "$1" = "rsync" ] || [ "$1" = "both" ]; then
for MACHINE in ${MACHINES}; do
        ./domounts.sh >> ${OUTPUTTEMP};
		# If machine is unreachable, don't try and sync.
		if [ -f "${MACHINE}_status_up.txt" ]
		then
        	rsyncPC2HERE ${MACHINE};
		fi
done
fi

if [ "$1" = "flip" ] || [ "$1" = "both" ]; then
	echo "Flipping ${ALLMACHINES}" >> ${OUTPUTTEMP}
	if [ -d ${DIR_STAGING} ];then
     		echo ${RM} -r ${DIR_STAGING}/*;
 	    	# $RM -r ${DIR_STAGING}/*;
	fi
	for MACHINE in ${ALLMACHINES}; do
		flip ${MACHINE};
	done	
fi

rm flip.running
